package hlhj.fhp.supreme.activitys

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import com.mylhyl.superdialog.SuperDialog
import hlhj.fhp.supreme.BaseAty
import hlhj.fhp.supreme.R
import hlhj.fhp.supreme.customview.ChooseGetCashPop
import hlhj.fhp.supreme.javabean.BaseBean
import hlhj.fhp.supreme.network.Urls
import hlhj.fhp.supreme.utils.JsonCallBack
import hlhj.fhp.supreme.utils.MyUtils
import kotlinx.android.synthetic.main.activity_get_cash_aty.*

class GetCashAty : BaseAty() {
    val CHOOSE_BANK=1
    private var getCashType=1;
    private lateinit var chooseGetCashPop: ChooseGetCashPop

    private var bid=""
    override fun getContentId(): Int {
        return R.layout.activity_get_cash_aty
    }

    override fun initView() {
        chooseGetCashPop= ChooseGetCashPop(this,object :ChooseGetCashPop.OnChooseGetCashListener{
            override fun onAli() {
                getCashType=2
                loBank.visibility= View.GONE
                loAli.visibility=View.VISIBLE
            }

            override fun onBank() {
                getCashType=1
                loBank.visibility= View.VISIBLE
                loAli.visibility=View.GONE
            }
        })
        chooseGetCashPop.setOnDismissListener {
            val lp = window.attributes
            lp.alpha = 1f
            window.attributes = lp
        }
    }
    override fun initListener() {

        btExit.setOnClickListener {
            finish()
        }
        btCommit.setOnClickListener {
            if (getCashType==1){
                if (bid.equals("")){
                    MyUtils.toast("请选择银行")
                }else {
                    if (etMoney.text.isEmpty()){
                        MyUtils.toast("请输入提现金额")
                    }else{
                        SuperDialog.Builder(this).setTitle("注意")
                                .setMessage("确认提现${etMoney.text}元到 ${defaultBank.text}账户？")
                                .setPositiveButton("确定"){
                                    OkGo.get<BaseBean>(Urls.BaseUrl+Urls.GETCASH)
                                            .params("bid",bid)
                                            .params("type",1)
                                            .params("money",etMoney.text.toString())
                                            .execute(object :JsonCallBack<BaseBean>(this){
                                                override fun onSuccess(response: Response<BaseBean>?) {
                                                    val body = response?.body()
                                                    if (body?.code==1){
                                                        MyUtils.toast("提现申请成功")
                                                        finish()
                                                    }else{
                                                        MyUtils.toast(body?.msg)
                                                    }
                                                }
                                            })
                                }
                                .setNegativeButton("取消"){

                                }
                                .build()
                    }
                }
            }else{
                if (etAli.text.isEmpty()){
                    MyUtils.toast("请输入淘宝账号")
                }else{
                    if (etMoney.text.isEmpty()){
                        MyUtils.toast("请输入提现金额")
                    }else{
                        SuperDialog.Builder(this).setTitle("注意")
                                .setMessage("确认提现${etMoney.text}元到 ${etAli.text}账户？")
                                .setPositiveButton("确定"){
                                    OkGo.get<BaseBean>(Urls.BaseUrl+Urls.GETCASH)
                                            .params("type",2)
                                            .params("alipay",etAli.text.toString())
                                            .params("money",etMoney.text.toString())
                                            .execute(object :JsonCallBack<BaseBean>(this){
                                                override fun onSuccess(response: Response<BaseBean>?) {
                                                    val body = response?.body()
                                                    if (body?.code==1){
                                                        MyUtils.toast("提现申请成功")
                                                        finish()
                                                    }else{
                                                        MyUtils.toast(body?.msg)
                                                    }
                                                }
                                            })
                                }
                                .setNegativeButton("取消"){

                                }
                                .build()
                    }
                }
            }
            /*if (bid.equals("")){
                MyUtils.toast("请选择银行")
            }else {
                if (etMoney.text.isEmpty()){
                    MyUtils.toast("请输入提现金额")
                }else{
                    OkGo.get<BaseBean>(Urls.BaseUrl+Urls.GETCASH)
                            .params("bid",bid)
                            .params("money",etMoney.text.toString())
                            .execute(object : JsonCallBack<BaseBean>(this){
                                override fun onSuccess(response: Response<BaseBean>?) {
                                    val body = response?.body()
                                    if (body?.code==1){
                                        MyUtils.toast("提现申请成功")
                                        finish()
                                    }else{
                                        MyUtils.toast(body?.msg)
                                    }

                                }
                            })
                }
            }*/
        }
        loBank.setOnClickListener {
            val intent = Intent(this, BankAty::class.java)
            startActivityForResult(intent,CHOOSE_BANK)
        }

    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode!=-1)return
        when(requestCode){
            CHOOSE_BANK->{
                defaultBank.text=data?.getStringExtra("name")
                bid=data?.getStringExtra("id")!!
            }
        }
    }
}
